package com.example.appkcsj.dbutils;


import com.example.appkcsj.entity.Student;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.distributed.common.Value;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DBUtils {
    private RdbStore rdbStore;
    private DatabaseHelper helper;
    public DBUtils(Context context){
        helper=new DatabaseHelper(context);
        initDB();
    }
    //初始化数据库
    private void initDB() {
        StoreConfig config=StoreConfig.newDefaultConfig("student.db");
        RdbOpenCallback callback=new RdbOpenCallback() {
            @Override
            public void onCreate(RdbStore rdbStore) {
                rdbStore.executeSql("create table if not exists student(id integer primary key autoincrement,"+
                        "username1 text not null,password1 text not null,password2 text not null)");
            }
            @Override
            public void onUpgrade(RdbStore rdbStore, int i, int i1) {
            }
        };
        rdbStore=helper.getRdbStore(config,1,callback);
    }
    public boolean insertStudent(Student student){
        ValuesBucket bucket =new ValuesBucket();
        bucket.putString("username1",student.getUsername1());
        bucket.putString("password1",student.getPassword1());
        bucket.putString("password2",student.getPassword2());
        long id=rdbStore.insert("student",bucket);
        return id>0?true:false;
    }
    public boolean updateStudent(Student student){
        RdbPredicates predicates=new RdbPredicates("student");
        predicates.equalTo("id",student.getId());
        ValuesBucket bucket=new ValuesBucket();
        bucket.putString("username1",student.getUsername1());
        bucket.putString("password1",student.getPassword1());
        bucket.putString("password2",student.getPassword2());
        int id =rdbStore.update(bucket,predicates);
        return id>0?true:false;
    }
    public boolean deleteStudent(String username){
        RdbPredicates predicates=new RdbPredicates("stduent");
        predicates.equalTo("username",username);
        int rowid=rdbStore.delete(predicates);
        return rowid>0?true:false;
    }
    public Student queryOneStudent(){
        String[] queryName=new String[]{
                "id","username1","password1","password2"
        };
        RdbPredicates predicates=new RdbPredicates("student");
        ResultSet resultSet=rdbStore.query(predicates,queryName);
        Student student=new Student();
        while (resultSet.goToNextRow()){
            student.setId(resultSet.getInt(resultSet.getColumnIndexForName("id")));
            student.setUsername1(resultSet.getString(resultSet.getColumnIndexForName("username1")));
            student.setPassword1(resultSet.getString(resultSet.getColumnIndexForName("password1")));
            student.setPassword2(resultSet.getString(resultSet.getColumnIndexForName("password2")));
        }
        return student;
    }
    public List<Student> queryMoreStudent(String username1){
        List<Student> list = new ArrayList<>();
        String[] queryName=new String[]{
                "id","username1","password1","password2"
        };
        RdbPredicates predicates=new RdbPredicates("student");
        predicates.like("username1",username1);
        ResultSet resultSet=rdbStore.query(predicates,queryName);
        while(resultSet.goToNextRow()){
            Student student=new Student();
            student.setId(resultSet.getInt(resultSet.getColumnIndexForName("id")));
            student.setUsername1(resultSet.getString(resultSet.getColumnIndexForName("username1")));
            student.setPassword1(resultSet.getString(resultSet.getColumnIndexForName("password1")));
            student.setPassword2(resultSet.getString(resultSet.getColumnIndexForName("password2")));
            list.add(student);
        }
        return list;
    }
}

